package com.deyuanyun.pic.pipe.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.converter.BeanConverter;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.pipe.controller.dto.AdjunctQuery;
import com.deyuanyun.pic.pipe.controller.vo.merge.AdjunctMergeVO;
import com.deyuanyun.pic.pipe.controller.vo.merge.AdjunctVo;
import com.deyuanyun.pic.pipe.enumeration.PipeExportEnum;
import com.deyuanyun.pic.pipe.service.AdjunctService;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * "附属物"控制类（包含点状附属物 和 线状附属物）
 *
 * @author hlw
 * @date 2016-08-25
 *
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/pipe/adjunct")
public class AdjunctController extends PipeBaseController {

	@Resource
	private AdjunctService adjunctService;

	/**
	 * 跳转到 附属物 主页面
	 *
	 * @return
     */
	@RequestMapping("/toAdjunct")
	public ModelAndView toAdjunct(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("/basedata/gis/adjunct");
		return mv;
	}

	/**
	 * 根据条件分页合并查询 附属物(包含点状附属物 和 线状附属物) 数组对象
	 *
	 * @param adjunctQuery
	 */
	@RequestMapping("/query.json")
	@ResponseBody
	public void query(AdjunctQuery adjunctQuery) {
		PageJsonBean jsonBean = new PageJsonBean();
		jsonBean.setList(new AdjunctMergeVO().fromDomainList(adjunctService.queryAdjunct(adjunctQuery)));
		AjaxSupport.sendSuccessText("success", jsonBean);
	}

	/**
	 * 根据条件 导出“附属物”
	 *
	 * @param adjunctQuery
	 * @return
	 */
	@RequestMapping("/export")
	@ResponseBody
	public void export(AdjunctQuery adjunctQuery) throws Exception{

		AdjunctVo adjunctVo = new AdjunctVo();
		Page<AdjunctVo> adjunctLineVoList = adjunctVo.forDomainLine(adjunctService.queryAdjunctLine(adjunctQuery));
		Page<AdjunctVo> adjunctPointVoList = adjunctVo.forDomainPoint(adjunctService.queryAdjunctPoint(adjunctQuery));

		List<ExcelUtil.SheetParam> list = new ArrayList<ExcelUtil.SheetParam>();
		String[] columnKeys =  {"pipeLineName", "customNumber", "appurtenanceType", "fromMileage", "fromReference",
				"fromReferenceDistance", "appurtenanceStatus", "recorder", "recordTime", "remark"};
		list.add(new ExcelUtil.SheetParam(columnKeys, BeanConverter.toListMap(adjunctPointVoList), 2));
		String[] lineColumnKeys = {"pipeLineName", "customNumber", "appurtenanceType", "fromMileage", "fromReference", "fromReferenceDistance",
				"toMileage", "toReference", "toReferenceDistance", "appurtenanceStatus", "recorder", "recordTime", "remark"};
		list.add(new ExcelUtil.SheetParam(lineColumnKeys, BeanConverter.toListMap(adjunctLineVoList), 2));

		PipeExportEnum pipeExportEnum = PipeExportEnum.Adjunct;
		DownExcelUtil.export(pipeExportEnum.getFileName(), pipePath+ pipeExportEnum.getTempPath(), list);
	}

}
